#include "leecode.h"

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
 
class Solution {
public:
  vector<int> inorderTraversal(TreeNode* root) {
    vector<TreeNode* > stack;
    vector<int> result;
    TreeNode* current = root;
    stack.push_back(current);
    while (!stack.empty()) {
      if (current->left) {
        current = current->left;
        stack.push_back(current);
      }
      else {
        TreeNode* temp = stack.back();
        // printf("%d ", temp->val);
        result.push_back(temp->val);
        stack.pop_back();
        if (temp->right) {
          current = temp->right;
          stack.push_back(temp->right);
        }
      }
    }

    return std::move(result);
  }
};